package com.xinsoft.controller.qua;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.QuaPatrolStandardParam;
import com.xinsoft.entity.po.QuaPatrolStandard;
import com.xinsoft.service.QuaPatrolStandardService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName QuaPatrolStandardController
 * @Description 巡检标准
 * @Author lijie
 * @Date 2023-8-1 10:05:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/qua/patrol/standard")
@Api(value = "QuaPatrolStandardController", tags = {"质量管理-巡检标准控制层"})
public class QuaPatrolStandardController {
	@Autowired
	private QuaPatrolStandardService standardService;


	/**
	 * @Author Li9527
	 * @Description 巡检标准-新增
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("新增")
	@PostMapping("/add")
	public Response<?> add(@RequestBody QuaPatrolStandard quaPatrolStandard) {
		// 参数校验
		ValidatorUtils.validateEntity(quaPatrolStandard);
		QuaPatrolStandard oldObj = standardService.getOne(new QueryWrapper<QuaPatrolStandard>().eq("standard_name",quaPatrolStandard.getStandardName()));
		if(oldObj != null){
			return Response.fail("该项巡检标准控已存在,请不要重复添加");
		}
		return Response.succeed(standardService.save(quaPatrolStandard));
	}

	/**
	 * @Author Li9527
	 * @Description 更新
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("更新")
	@PostMapping("/edit")
	public Response<?> edit(@RequestBody QuaPatrolStandard quaPatrolStandard) {
		// 参数校验
		ValidatorUtils.validateEntity(quaPatrolStandard);
		QuaPatrolStandard oldObj = standardService.getOne(new QueryWrapper<QuaPatrolStandard>().eq("standard_name",quaPatrolStandard.getStandardName()));
		if(oldObj != null && !oldObj.getId().equals(quaPatrolStandard.getId())){
			return Response.fail("该项巡检标准控已存在,请不要重复添加");
		}
		return Response.succeed(standardService.updateById(quaPatrolStandard));
	}

	/**
	 * @Author Li9527
	 * @Description 获取详情
	 * @Date 2023/5/17 9:57
	 * @params   id
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("获取详情")
	@GetMapping("/detail")
	public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
		return Response.succeed(standardService.getById(id));
	}

	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("删除")
	@PostMapping("/del")
	public Response<?> del(@RequestBody List<Integer> ids) {
		standardService.removeByIds(ids);
//		for (int i = 0; i < ids.size(); i++) {
//			refService.remove(new QueryWrapper<CusDevicePatrolStandardRef>().eq("patrol_standard_id",ids.get(i)));
//		}
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 巡检标准控分页
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/pageList")
	public Response<?> pageList(@Valid @RequestBody QuaPatrolStandardParam param) {
		return Response.succeed(standardService.findPageListByParam(param));
	}
}
